package com.example.demo_text;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CommentDAO {
    // Database connection string (using the same approach as other DAOs)
    private static final String JDBC_URL = databaseaccess.DatabaseUrl2;
    private static final String JDBC_USER = databaseaccess.DatabaseUser2;
    private static final String JDBC_PASSWORD =  databaseaccess.DatabasePassword2;

    // Get a database connection
    private static Connection getConnection() throws SQLException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            return DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
        } catch (ClassNotFoundException e) {
            throw new SQLException("Database driver not found", e);
        }
    }

    // Close database resources
    private static void closeResources(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // Add a new comment to an answer
    public static boolean addComment(int ansId, String commentContent) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = getConnection();
            String sql = "INSERT INTO comments (ans_id, comment_content, comment_time) VALUES (?, ?, NOW())";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, ansId);
            stmt.setString(2, commentContent);
            
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(conn, stmt, null);
        }
    }

    // Get all comments for a specific answer
    public static List<Comment> getCommentsByAnswerId(int ansId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Comment> comments = new ArrayList<>();

        try {
            conn = getConnection();
            String sql = "SELECT * FROM comments WHERE ans_id = ? ORDER BY comment_time";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, ansId);
            rs = stmt.executeQuery();
            
            while (rs.next()) {
                Comment comment = new Comment(
                    rs.getInt("comment_id"),
                    rs.getInt("ans_id"),
                    rs.getString("comment_content"),
                    rs.getTimestamp("comment_time")
                );
                comments.add(comment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(conn, stmt, rs);
        }
        
        return comments;
    }

    // Delete a comment
    public static boolean deleteComment(int commentId) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = getConnection();
            String sql = "DELETE FROM comments WHERE comment_id = ?";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, commentId);
            
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(conn, stmt, null);
        }
    }
} 